//#include <QCoreApplication>
#include <list>
#include <vector>
#include <string>
#include "utils.h"
#include "FileOperate.h"

void doldd(std::vector<std::string>& sovec, std::vector<std::string>& retldd)
{
    retldd.clear();
    for(size_t i = 0; i < sovec.size(); ++i)
    {
        const std::string& strso = sovec[i];
        char szCmd[512] = {0};
        sprintf(szCmd,"ldd %s > tmp.txt",strso.c_str());
        system(szCmd);
        std::vector<std::string> lddstr = FileOperate::readFileLine("./tmp.txt");
        for(int j = 0; j < lddstr.size(); ++j)
        {
            std::string str = lddstr[j];
            if(Utils::getSubStringCount(str,"=>") > 0)
            {
                std::vector<std::string> strSplit;
                Utils::splitString(str,"=>",strSplit);
                if(strSplit.size() == 2)
                {
                    std::string strInfo = strSplit[1];
                    if(Utils::getSubStringCount(strInfo,"(") > 0)
                    {
                        strSplit.clear();
                        Utils::splitString(strInfo,"(",strSplit);
                        strInfo = strSplit[0];
                        Utils::trimString(strInfo);

                        memset(szCmd,0,sizeof(szCmd));
                        sprintf(szCmd,"cp %s ./lddso",strInfo.c_str());
                        system(szCmd);
                        retldd.push_back(strInfo);
                        std::cout << strInfo << std::endl;
                    }
                }
            }
        }
    }
}


int main(int argc, char *argv[])
{
//    QCoreApplication a(argc, argv);

    if(argc < 2)
    {
        return 0;
    }
    std::vector<std::string> sovec;
    for(int i = 1; i< argc; ++i)
    {
        sovec.push_back(argv[i]);
    }

    system("mkdir ./lddso");

    std::vector<std::string> retlddso;
    doldd(sovec,retlddso);

    std::vector<std::string> retlddso1;
    doldd(retlddso,retlddso1);

    std::vector<std::string> retlddso2;
    doldd(retlddso1,retlddso2);

    std::vector<std::string> retlddso3;
    doldd(retlddso2,retlddso3);

    std::vector<std::string> retlddso4;
    doldd(retlddso3,retlddso4);


    std::cout << "Finished!" << std::endl;
//    return a.exec();
}
